package com.ruoyi.smartcity.huanjing.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.smartcity.huanjing.domain.HuanjingKongqizhiliang;
import com.ruoyi.smartcity.huanjing.mapper.HuanjingKongqizhiliangMapper;
import com.ruoyi.smartcity.huanjing.service.IHuanjingKongqizhiliangService;
import com.ruoyi.smartcity.traffic.vo.EchartsOptionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 空气质量Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-06
 */
@Service
public class HuanjingKongqizhiliangServiceImpl implements IHuanjingKongqizhiliangService {
    @Autowired
    private HuanjingKongqizhiliangMapper huanjingKongqizhiliangMapper;

    /**
     * 查询空气质量
     *
     * @param id 空气质量主键
     * @return 空气质量
     */
    @Override
    public HuanjingKongqizhiliang selectHuanjingKongqizhiliangById(Long id) {
        return huanjingKongqizhiliangMapper.selectHuanjingKongqizhiliangById(id);
    }

    /**
     * 查询空气质量列表
     *
     * @param huanjingKongqizhiliang 空气质量
     * @return 空气质量
     */
    @Override
    public List<HuanjingKongqizhiliang> selectHuanjingKongqizhiliangList(HuanjingKongqizhiliang huanjingKongqizhiliang) {
        return huanjingKongqizhiliangMapper.selectHuanjingKongqizhiliangList(huanjingKongqizhiliang);
    }

    /**
     * 新增空气质量
     *
     * @param huanjingKongqizhiliang 空气质量
     * @return 结果
     */
    @Override
    public int insertHuanjingKongqizhiliang(HuanjingKongqizhiliang huanjingKongqizhiliang) {
        huanjingKongqizhiliang.setCreateTime(DateUtils.getNowDate());
        return huanjingKongqizhiliangMapper.insertHuanjingKongqizhiliang(huanjingKongqizhiliang);
    }

    /**
     * 修改空气质量
     *
     * @param huanjingKongqizhiliang 空气质量
     * @return 结果
     */
    @Override
    public int updateHuanjingKongqizhiliang(HuanjingKongqizhiliang huanjingKongqizhiliang) {
        huanjingKongqizhiliang.setUpdateTime(DateUtils.getNowDate());
        return huanjingKongqizhiliangMapper.updateHuanjingKongqizhiliang(huanjingKongqizhiliang);
    }

    /**
     * 批量删除空气质量
     *
     * @param ids 需要删除的空气质量主键
     * @return 结果
     */
    @Override
    public int deleteHuanjingKongqizhiliangByIds(Long[] ids) {
        return huanjingKongqizhiliangMapper.deleteHuanjingKongqizhiliangByIds(ids);
    }

    /**
     * 删除空气质量信息
     *
     * @param id 空气质量主键
     * @return 结果
     */
    @Override
    public int deleteHuanjingKongqizhiliangById(Long id) {
        return huanjingKongqizhiliangMapper.deleteHuanjingKongqizhiliangById(id);
    }

    @Override
    public EchartsOptionVO kongqizhiliang() {
        EchartsOptionVO echartsOptionVO = new EchartsOptionVO();
        List<HuanjingKongqizhiliang> list = huanjingKongqizhiliangMapper.selectHuanjingKongqizhiliangList(new HuanjingKongqizhiliang());
        if (list != null && !list.isEmpty()) {
            JSONArray yData = new JSONArray();
            JSONArray xData = new JSONArray();
            for (HuanjingKongqizhiliang item : list) {

                xData.add(item.getName());
                yData.add(item.getMonitorValue());
                echartsOptionVO.setXData(xData);
                echartsOptionVO.setYData(yData);
            }
        }
        return echartsOptionVO;
    }
}
